package com.brillilab.starter.controller.aio.method;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.method.CreateMethodRecommend;
import com.brillilab.domain.vo.method.MethodRecommendDeal;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.method.MethodRecommendLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wuzhiyu
 * @Title: MethodRecommendController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/2/2110:06
 */
@RestController
@RequestMapping("/api/methodRecommend")
public class MethodRecommendController {

    @Resource
    private MethodRecommendLogic methodRecommendLogic;
    @Resource
    private UsersLogic usersLogic;

    @AspectLog
    @PostMapping("/recommend")
    public ResponseVo recommendAudit(@RequestBody CreateMethodRecommend recommend, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        UserInfoVo user = usersLogic.getUserByToken(token);
        Assert.notNull(recommend, "参数缺失");
        Assert.isTrue(!CollectionUtils.isEmpty(recommend.getMethodIdList()), "参数缺失");
        Assert.notNull(recommend.getMethodIdList(), "参数缺失");
        return methodRecommendLogic.createRecommendAudit(recommend, user) ? ResponseVo.success() : ResponseVo.failure();
    }

    @GetMapping("/recommendInfo")
    public ResponseVo recommendInfo(Long recommendId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(recommendId, "参数缺失");
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodRecommendLogic.getRecommendInfo(recommendId, user));
    }

    @PostMapping("/recommendDeal")
    public ResponseVo recommendDeal(@RequestBody MethodRecommendDeal deal, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.notNull(deal, "参数缺失");
        Assert.notNull(deal.getRecommendId(), "参数缺失");
        Assert.notNull(deal.getState(), "参数缺失");
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodRecommendLogic.recommendDeal(deal, user));
    }

    @GetMapping("/myRecommendList")
    public ResponseVo myRecommendList(Integer pageIndex, Integer rows, Integer state,@RequestHeader(value = StarterConstant.TOKEN) String token) {
        pageIndex = pageIndex == null ? 1 : pageIndex;
        rows = rows == null ? 10 : rows;
        UserInfoVo user = usersLogic.getUserByToken(token);
        return ResponseVo.success(methodRecommendLogic.listMyRecommend(user, pageIndex, rows,state));
    }
    @GetMapping("/auditResult")
    public ResponseVo auditResult(Long auditId,@RequestHeader(value = StarterConstant.TOKEN) String token){
        Assert.notNull(auditId,"参数缺失");
        UserInfoVo user= usersLogic.getUserByToken(token);
        return ResponseVo.success(methodRecommendLogic.getAuditResult(user,auditId));
    }
}
